package cn.allspark.service.impl;

import cn.allspark.entity.po.UserPO;
import cn.allspark.mapper.UserMapper;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.allspark.service.UserService;

@Service
@NoArgsConstructor
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    //纯数字
    String pattern = "^\\d*$";

    /**
     * 添加用户, 如果存在就直接覆盖
     * @param user
     * @return 0 表示失败，1 表示成功
     */
    @Override
    public int addUser(UserPO user) {
        if(user == null)
            return 0;
        String tel = user.getPhoneNumber();
        String qq = user.getQqNumber();
        if(!(tel.length() == 11 && tel.matches(pattern) && qq.matches(pattern)))
            return 0;
        return userMapper.addUser(user);
    }

    /**
     * 根据手机号和QQ号查找用户
     * @param tel
     * @param qq
     * @return UserPO
     */
    @Override
    public UserPO searchUser(String tel,String qq) {
        // TODO 合法校验
        if(tel == null || qq == null)
            return null;
        if(!(tel.length() == 11 && tel.matches(pattern) && qq.matches(pattern)))
            return null;
        return userMapper.searchUser(tel, qq);
    }
}
